﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Itop.YHPC.controller;
using Itop.YHPC.Model;
using System.Collections.Generic;

namespace Itop.YHPC.workflow
{
    public partial class workflow_task_ajax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataRow[] rows = BasePage.GetUserProgs(null).Select("ParentID='1'");
            int taskCount = 0, zlCount = 0, ysCount = 0, temp = 0, dwcount = 0,firsttaskindext=100,firstdwindext=102;
            bool firsttaskflag = false, firstdwflag = false;
            foreach (DataRow prog in rows)
            {
                string wfid = prog["id"].ToString();
                if (BasePage.GetUser().LoginID == "admin")
                {
                    if (wfid == "200" || wfid == "201")
                    {
                        temp = workflowController.GetMainRowCount("where zlflag='" + wfid + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                        if (wfid == "200")
                            zlCount += temp;
                        else
                            ysCount += temp;
                    }
                    else
                    {
                        taskCount += workflowController.GetMainRowCount("where lcflag='" + wfid + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                        if (!firsttaskflag)
                        {
                            if (taskCount > 0)
                            {
                                firsttaskflag = true;
                                firsttaskindext = Convert.ToInt32(wfid);
                                 
                            }
                        }
                       
                        if (wfid == "102" || wfid == "103" || wfid == "104")
                        {
                            dwcount += workflowController.GetMainRowCount("where lcflag='" + wfid + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                            if (!firstdwflag)
                            {
                                if (dwcount>0)
                                {
                                    firstdwflag = true;
                                    firstdwindext = Convert.ToInt32(wfid);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (wfid == "101" || wfid == "100")
                    {
                        taskCount += workflowController.GetMainRowCount("where lcflag='" + wfid + "' and tjr='" + (Session["user"] as sysuser).UserName + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                        if (!firsttaskflag)
                        {
                            if (taskCount > 0)
                            {
                                firsttaskflag = true;
                                firsttaskindext = Convert.ToInt32(wfid);

                            }
                        }
                    }
                       
                    else if (wfid == "105" || wfid == "106")
                    {
                        taskCount += workflowController.GetMainRowCount("where lcflag='" + wfid + "' and gsbm='" + GetGSBM() + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                        if (!firsttaskflag)
                        {
                            if (taskCount > 0)
                            {
                                firsttaskflag = true;
                                firsttaskindext = Convert.ToInt32(wfid);

                            }
                        }
                    }
                       
                    else if (wfid == "200" || wfid == "201" )
                    {
                        if (wfid == "200")
                            zlCount += workflowController.GetMainRowCount("where zlflag='" + wfid + "'  and ((zldw='' and yhdw='" + getdw() + "') or zldw='" + getdw() + "' )  and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                        else
                            ysCount += workflowController.GetMainRowCount("where zlflag='" + wfid + "' and yszzdw='" + getdw() + "'  and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                    }
                    else if (wfid == "102" || wfid == "103" || wfid == "104")
                    {
                        taskCount += workflowController.GetMainRowCount("where lcflag='" + wfid + "' and fxrdw='" + getdw() + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                        if (!firsttaskflag)
                        {
                            if (taskCount > 0)
                            {
                                firsttaskflag = true;
                                firsttaskindext = Convert.ToInt32(wfid);

                            }
                        }
                        dwcount += workflowController.GetMainRowCount("where lcflag='" + wfid + "' and fxrdw='" + getdw() + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                        if (!firstdwflag)
                        {
                            if (dwcount > 0)
                            {
                                firstdwflag = true;
                                firstdwindext = Convert.ToInt32(wfid);
                            }
                        }
                    }
                    else
                    {
                        taskCount += workflowController.GetMainRowCount("where lcflag='" + wfid + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' ");
                        if (!firsttaskflag)
                        {
                            if (taskCount > 0)
                            {
                                firsttaskflag = true;
                                firsttaskindext = Convert.ToInt32(wfid);

                            }
                        }
                    }
                   
                
                }

            }
            if (BasePage.GetUserDw().dwType == "二级单位")
                ysCount = 0;
            string rst = taskCount.ToString() + "|" + zlCount.ToString() + "|" + ysCount.ToString() + "|" + dwcount.ToString() + "|" + firsttaskindext.ToString() + "|" + firstdwindext.ToString() + "<++>";
            Response.Write(rst);
        }

        private string getdw()
        {
            string dw = BasePage.GetUserDw().dwOwner;

            return dw;

        }
        public string GetGSBM()
        {
            IList<yhpcOrg> list = yhpcOrgController.GetList(" where id=(select dwid from sysuser where loginid='" + (Session["user"] as sysuser).LoginID + "')");
            if (list.Count > 0)
                return list[0].dwOwner;
            else
                return "";
        }
    }
}
